package com.app.qbake.module.mine.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.wind.base.adapter.BaseRecyclerAdapter;
import com.wind.data.mine.bean.Pkg;
import com.app.qbake.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VipPkgAdapter extends BaseRecyclerAdapter<Pkg,VipPkgAdapter.ViewHolder> {


    public VipPkgAdapter(Activity activity, int layoutRes) {
        super(activity, layoutRes);
    }

    @Override
    public ViewHolder onCreateViewHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Pkg item=getItem(position);
        holder.tv_pkg_name.setText(item.getTitle());
        holder.tv_pkg_price.setText(item.getPrice());

        if (item.isSelected()){
            holder.itemView.setActivated(true);
        }else {
            holder.itemView.setActivated(false);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i=0;i<getItemCount();i++){
                    Pkg pkg=getItem(i);
                    if (i==position){
                        pkg.setSelected(true);
                    }else {
                        pkg.setSelected(false);
                    }
                }
                notifyDataSetChanged();
            }
        });
    }

    public Pkg getSelectedItem(){
        for (int i=0;i<getItemCount();i++){
            Pkg pkg=getItem(i);
            if (pkg.isSelected()){
                return pkg;
            }
        }
        return null;

    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_pkg_name)
        TextView tv_pkg_name;
        @BindView(R.id.tv_pkg_price)
        TextView tv_pkg_price;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
